<template>
  <Header :title="'登录'" />
  <div class="login">
    <div class="login_box">
      <div class="login_title">欢迎登录</div>
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="username"
            name="用户名"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="密码"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
        </van-cell-group>
        <div style="display: flex; margin: 5px 32px">
          <span class="login_tap" @click="toRegister">没有账号？立即注册</span>
        </div>
        <div style="margin: 16px">
          <van-button round block type="primary" native-type="submit"> 登录 </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs } from "vue";
import Header from "@/components/SHeader.vue";
import { useRouter } from "vue-router";
import { getLogin } from "@/api/user";
import { toast } from "@/util/toast";
export default {
  components: {
    Header,
  },
  setup() {
    const router = useRouter();
    const loginInfo = reactive({
      username: "",
      password: "",
    });
    const onSubmit = async () => {
      const { data } = await getLogin(loginInfo);
      if (data.code == "200") {
        console.log(111111);
        localStorage.setItem("token", data.token);
        router.push("/home");
      } else {
        toast("fail", data.msg, 500);
      }
    };
    const toRegister = () => {
      router.push("/register");
    };
    return {
      ...toRefs(loginInfo),
      toRegister,
      onSubmit,
    };
  },
};
</script>
<style lang="scss">
.login {
  height: 100%;
}

.login_box {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
}

.login_title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 20px;
}

.login_tap {
  text-align: left;
  cursor: pointer;
}

.login_tap:hover {
  color: gray;
}
</style>
